import requests
import random
import tkinter as tk
from tkinter import filedialog, messagebox
from selenium import webdriver
from selenium.webdriver.edge.service import Service

# 全局变量保存 driver
driver_instance = None

# 获取随机代理
def get_random_proxy():
    api_url = 'https://service.ipzan.com/core-extract?num=1&no=20230525165982903521&minute=1&format=json&protocol=1&pool=quality&secret=uvg8pco497ldfag'
    response = requests.get(api_url, timeout=10)
    ip_list = response.json().get('data', {}).get('list', [])
    if not ip_list:
        raise Exception("没有可用的代理")
    proxy_info = random.choice(ip_list)
    return f"http://{proxy_info['ip']}:{proxy_info['port']}"

# 启动浏览器访问 URL
def start_browser(edge_path, driver_path, use_proxy, url):
    global driver_instance
    if not url.startswith("http"):
        messagebox.showerror("错误", "请填写有效的 URL")
        return
    options = webdriver.EdgeOptions()
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_argument("--ignore-certificate-errors")
    options.binary_location = edge_path

    if use_proxy:
        proxy = get_random_proxy()
        options.add_argument(f"--proxy-server={proxy}")
        print(f"正在使用代理: {proxy}")

    service = Service(driver_path)
    driver_instance = webdriver.Edge(service=service, options=options)
    driver_instance.get(url)
    print("浏览器已打开，可以操作网页。")

# 关闭浏览器
def close_browser():
    global driver_instance
    if driver_instance:
        driver_instance.quit()
        driver_instance = None
        messagebox.showinfo("提示", "浏览器已关闭！")
    else:
        messagebox.showinfo("提示", "浏览器未打开！")

# GUI 界面
def browse_edge():
    path = filedialog.askopenfilename(title="选择 Edge 浏览器", filetypes=[("exe 文件", "*.exe")])
    if path:
        edge_entry.delete(0, tk.END)
        edge_entry.insert(0, path)

def browse_driver():
    path = filedialog.askopenfilename(title="选择 Edge Driver", filetypes=[("exe 文件", "*.exe")])
    if path:
        driver_entry.delete(0, tk.END)
        driver_entry.insert(0, path)

def start():
    edge_path = edge_entry.get()
    driver_path = driver_entry.get()
    url = url_entry.get()
    use_proxy = proxy_var.get()
    if not edge_path or not driver_path or not url:
        messagebox.showerror("错误", "请填写 Edge 浏览器、驱动路径和 URL！")
        return
    start_browser(edge_path, driver_path, use_proxy, url)

root = tk.Tk()
root.title("问卷访问工具")

tk.Label(root, text="问卷 URL:").grid(row=0, column=0)
url_entry = tk.Entry(root, width=50)
url_entry.grid(row=0, column=1, columnspan=2)

tk.Label(root, text="Edge 浏览器路径:").grid(row=1, column=0)
edge_entry = tk.Entry(root, width=50)
edge_entry.grid(row=1, column=1)
tk.Button(root, text="浏览", command=browse_edge).grid(row=1, column=2)

tk.Label(root, text="Edge Driver 路径:").grid(row=2, column=0)
driver_entry = tk.Entry(root, width=50)
driver_entry.grid(row=2, column=1)
tk.Button(root, text="浏览", command=browse_driver).grid(row=2, column=2)

proxy_var = tk.BooleanVar()
tk.Checkbutton(root, text="使用代理", variable=proxy_var).grid(row=3, column=1)

tk.Button(root, text="启动浏览器", command=start).grid(row=4, column=0, pady=10)
tk.Button(root, text="关闭浏览器", command=close_browser).grid(row=4, column=1, pady=10)

root.mainloop()
#
#
# https://www.wjx.cn/vm/e8pnohT.aspx
# C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe
# G:\msedgedriver.exe